import Mock from "mockjs";

let { data } = Mock.mock({
  "data|126": [
    {
      name: "@cname",
      "sex|1": ["男", "女"],
      "age|1-150": 1,
      "phone|13000000000-18999999999": 13000000000,
      "id|10000000000000-99999999999999": 10000000000000,
      "ip|1": [
        "绿城留香园",
        "绿城丽江公寓（北区）",
        "滨江金色黎明（三期）",
        "旺田商务楼",
        "千峰公寓",
      ],
      time: "@date",
      "long|1": ["长租", "短租"],
      "peo|1": ["王大丫", "王二丫", "王三丫"],
      "city|1": ["上城区", "下沙区", "西湖区", "拱墅区"],
    },
  ],
});

Mock.mock("/api/vip", "post", (options) => {
  const { pageSize, current, city, long } = JSON.parse(options.body);

  console.log(city, long);
  

  if (pageSize && current) {
    let result;
    let count = 0;

    if (city && long) {

      console.log(111);
      
      result = data.filter(
        (item: any) => item.city === city && item.long === long
      );
      result = result.slice((current - 1) * pageSize, current * pageSize);
      console.log(result);
      
      count = data.filter(
        (item: any) => item.city === city && item.long === long
      ).length;
    } else {
      result = data.slice((current - 1) * pageSize, current * pageSize);
      count = data.length;
    }
    console.log(result);
    return {
      status: 0,
      result: result,
      count: data.length,
    };
  } else {
    return {
      status: 1,
      result: "参数错误",
    };
  }
});

Mock.mock("/api/vip/sousuo", "post", (options) => {
  const { pageSize, current, sousuo } = JSON.parse(options.body);


  if (pageSize && current) {
    let result;
    let count = 0;

    
     if(sousuo){
      result = data.slice( (current-1) * pageSize, current*pageSize )
      result = data.filter((item:any) => item.id === +sousuo)
      count = data.filter((item:any) => item.id === +sousuo).length
     }else{
      result = data.slice( (current-1) * pageSize, current*pageSize )
      count = data.length
     }
    console.log(result);
    return {
      status: 0,
      result: result,
      count: data.length,
    };
  } else {
    return {
      status: 1,
      result: "参数错误",
    };
  }
});





Mock.mock("/api/pos/del", "post", (options) => {
  const { id } = JSON.parse(options.body);
  if (id) {
    data = data.filter((item: any) => item.id !== id);
    return {
      status: 0,
      msg: "删除成功",
    };
  } else {
    return {
      status: 1,
      msg: "参数错误",
    };
  }
});

Mock.mock("/api/vip/add", "post", (options) => {
  const { id, name, ip } = JSON.parse(options.body);
  console.log(JSON.parse(options.body));

  if (id && name && ip) {
    data.unshift(JSON.parse(options.body));
    return {
      status: 0,
      msg: "添加成功",
    };
  } else {
    return {
      status: 1,
      msg: "参数错误,必须有姓名、收入和订单类型数据",
    };
  }
});

export default Mock;
